package edu.nf.vac.users.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import edu.nf.vac.users.dao.UserDao;
import edu.nf.vac.users.dto.PageDTO;
import edu.nf.vac.users.dto.UserDTO;
import edu.nf.vac.users.entity.User;
import edu.nf.vac.users.exception.ChangePasswordException;
import edu.nf.vac.users.exception.UserException;
import edu.nf.vac.users.inf.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : QYM
 * @create 2022/3/29
 * 用户服务实现类
 */

@DubboService(interfaceClass = UserService.class,version = "1.0.0")
@Slf4j
@Transactional(rollbackFor = RuntimeException.class)

public class UserServiceImpl implements UserService {
    /**
     * 注入userDao
     */
    @Autowired
    private UserDao userDao;

    /**
     * 添加用户
     * @param userDTO
     */
    @Override
    public void addUser(UserDTO userDTO) {
        try {
            //拷贝User对象（连带所有属性一起拷贝）
            User user = BeanUtil.copyProperties(userDTO, User.class);
            userDao.addUser(user);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new UserException("添加用户失败",e);
        }
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void delUser(int id) {
        try {
            userDao.delUser(id);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new UserException("删除用户失败",e);
        }
    }

    /**
     * 更新用户
     * @param userDTO
     */
    @Override
    public void updateUser(UserDTO userDTO) {
        try {
            User user = BeanUtil.copyProperties(userDTO,User.class);
            userDao.updateUser(user);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new UserException("更新用户信息失败",e);
        }
    }

    /**
     * 修改密码
     * @param userDTO
     * @param oldPass
     * @param newPass
     */
    @Override
    public void changePwd(UserDTO userDTO, String oldPass, String newPass) {
        //判断用户是否为null，并且所获取的密码是否与原密码相同
        if (userDTO != null && !userDTO.getUserPassword().equals(oldPass)){
            throw new ChangePasswordException("原密码错误");
        }
        userDao.changePwd(userDTO.getUid(),newPass);
    }

    /**
     * 修改用户状态
     * @param userId
     * @param userStatus
     */
    @Override
    public void changeUserStatus(int userId, boolean userStatus) {
        //boolean默认为false
        userDao.changeUserStatus(userId,!userStatus);
    }

    /**
     * 违约次数
     * @param userId
     */
    @Override
    public void breakPromise(int userId) {
        userDao.updateBreakCount(userId);
    }

    /**
     * 查询用户列表
     * @param userDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageDTO<UserDTO> listUser(UserDTO userDTO, int pageNum, int pageSize) {
        User user = BeanUtil.copyProperties(userDTO, User.class);
        List<User> list = userDao.listUser(user, pageNum, pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        PageDTO pageDTO = BeanUtil.copyProperties(pageInfo, PageDTO.class);
        return pageDTO;
    }

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserDTO getUserById(int id) {
        User user = userDao.getUserById(id);
        UserDTO dto = BeanUtil.copyProperties(user, UserDTO.class);
        return dto;
    }

    /**
     * 根据电话查询用户信息
     * @param tel
     * @return
     */
    @Override
    public UserDTO getUserByTel(String tel) {
        User user = userDao.getUserByTel(tel);
        UserDTO dto = BeanUtil.copyProperties(user, UserDTO.class);
        return dto;
    }

    /**
     * 根据用户名查询用户信息
     * @param userName
     * @return
     */
    @Override
    public List<UserDTO> queryUserByName(String userName) {
        List<UserDTO> userDTOList = new ArrayList<>();
        List<User> list = userDao.getUserByName(userName);
        for (User user : list) {
            UserDTO dto = BeanUtil.copyProperties(user, UserDTO.class);
            userDTOList.add(dto);
        }
        return userDTOList;
    }
}
